//
//  WalletCentreView.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/1/3.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit
protocol WalletCentreViewDelegate:NSObjectProtocol {
    func payActionDele(productId:String, price:String)
}

class WalletCentreView: UIView {
    
    var delegate:WalletCentreViewDelegate?
    var index:Int = 0
    var id:String?
    var price:String?
    fileprivate let WalletCentreCellID = "WalletCentreCell"
    
    var model = [ExchangeRateModel]()
    var modelList: [ExchangeRateModel]? {
        didSet {
            guard let model = modelList else { return }
            self.model = model
            self.collectionView.reloadData()
        }
    }
    
    fileprivate lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize.init(width: (SCREEN_WIDTH - 60) / 3, height: 71)
//        layout.headerReferenceSize = CGSize.init(width: SCREEN_WIDTH, height: 55)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 12.5
        layout.sectionInset = UIEdgeInsets.init(top: 0, left: 17.5, bottom: 0, right: 17.5)
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.register(WalletCentreCell.self, forCellWithReuseIdentifier: WalletCentreCellID)
        collectionView.delegate = self
        collectionView.dataSource = self
        return collectionView
    }()

    init(frame: CGRect, height:CGFloat) {
        super.init(frame: frame)
        self.collectionView.frame = FRAME_CGFloat(0, 40, SCREEN_WIDTH, height)
        self.collectionView.backgroundColor = WhiteBackColor
        self.addSubview(self.collectionView)
        
        let leftLabel = UILabel.init(frame: FRAME_CGFloat(17.5, 20, 41, 18.5))
        leftLabel.text = "充听币"
        leftLabel.font = Font_Medium(13)
        leftLabel.textColor = RGB_Same(53)
        self.addSubview(leftLabel)
        
        let rightLabel = UILabel.init(frame: FRAME_CGFloat(80.5, 20, 250, 20))
        rightLabel.text = "马上充值做超级用户，享更多福利！"
        rightLabel.font = Font_Medium(14)
        rightLabel.textColor = RGB(230, 67, 64)
        self.addSubview(rightLabel)
        
        let payBtn = UIButton.init(type: .custom)
        payBtn.frame = FRAME_CGFloat(37.5, 60 + height, SCREEN_WIDTH - 75, 44)
        payBtn.setBackgroundImage(IMAGE(name: "Rectangle"), for: .normal)
        payBtn.setTitle("立即支付", for: .normal)
        payBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
        payBtn.addTarget(self, action: #selector(payAction), for: .touchUpInside)
        self.addSubview(payBtn)
    }
    
    @objc func payAction() {
        delegate?.payActionDele(productId: self.id ?? "", price: self.price ?? "")
    }
 
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

//  MARK: - ###### 协议方法 ######
extension WalletCentreView:UICollectionViewDelegate,UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.model.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: WalletCentreCellID, for: indexPath) as! WalletCentreCell
        cell.topLabel.text = "\(String.moneyToInt(self.model[indexPath.row].tingCoin) ?? "--")听币"
        cell.centreLabel.text = "\(String.moneyToInt(self.model[indexPath.row].cnyCoin) ?? "--")元"
        if self.model[indexPath.row].hasDiscount {
            cell.bottomLabel.text = "多送\(String.moneyToInt(self.model[indexPath.row].discountValue) ?? "--")元"
        } else {
            cell.bottomLabel.text = ""
        }
        if self.model[indexPath.row].sellWell {
            cell.imageView.isHidden = false
        } else {
            cell.imageView.isHidden = true
        }
        
        if indexPath.row == index {
            cell.setSelector()
            self.id = self.model[indexPath.row].iosParam
            self.price = "\(String.moneyToInt(self.model[indexPath.row].cnyCoin) ?? "")"
        } else {
            cell.setNoSelector()
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.index = indexPath.row
        self.collectionView.reloadData()
  
    }
}
